package com.opd.soeghtool;

import android.app.Activity;
import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

public class USBDebugActivity extends Activity {

    TextView tv_usb_debug_permit,tv_usb_debug_device_list,tv_usb_debug_ep,tv_usb_debug_my_device;
    GlobalVar mGlobalVar;
    int mCounter=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usb_debug_layout);
        mGlobalVar = (GlobalVar)getApplication();
        tv_usb_debug_permit = findViewById(R.id.tv_usb_debug_permit);
        tv_usb_debug_device_list = findViewById(R.id.tv_usb_debug_device_list);
        tv_usb_debug_ep = findViewById(R.id.tv_usb_debug_ep);
        tv_usb_debug_my_device = findViewById(R.id.tv_usb_debug_my_device);
        mCounter = 0;
        if( mGlobalVar.myUsbManager!=null)
        {
            HashMap<String, UsbDevice> deviceList = mGlobalVar.myUsbManager.getDeviceList();
            if (!deviceList.isEmpty()) {
                StringBuffer sb = new StringBuffer();
                for (UsbDevice device : deviceList.values()) {
                    mCounter++;
                    sb.append(String.format("USB%02d: ",mCounter));
                    sb.append("\n");
                    sb.append(device.toString());
                    sb.append("\n");
                    // 枚举到设备
                }
                tv_usb_debug_device_list.setText(sb);
            }

            if(mGlobalVar.myUsbDevice!=null)
            {
                String str = "当前设备: "+ mGlobalVar.myUsbDevice.getProductName()+"  "+String.format("ID=%x,%x",mGlobalVar.myUsbDevice.getVendorId(),mGlobalVar.myUsbDevice.getProductId());
                tv_usb_debug_my_device.setText(str);
                if(mGlobalVar.myUsbManager.hasPermission(mGlobalVar.myUsbDevice))
                {
                    tv_usb_debug_permit.setText("当前设备: 已授权");
                }
                else
                {
                    tv_usb_debug_permit.setText("当前设备: 未授权");
                }
                tv_usb_debug_ep.setText("接口数目: "+String.valueOf(mGlobalVar.myUsbDevice.getInterfaceCount()));
            }
            else
            {
                tv_usb_debug_my_device.setText("没有找到匹配设备");
            }


        }
        else
        {
            tv_usb_debug_device_list.setText("没有找到USB管理器");
        }



    }
}
